package org.csu.demo.controller.front;

import org.csu.demo.common.CommonResponse;
import org.csu.demo.common.ResponseCode;
import org.csu.demo.service.AccountService;
import org.csu.demo.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping("/login")
    @ResponseBody
    public CommonResponse<AccountVO> login(@RequestParam("username") String username,
                                           @RequestParam("password") String password,
                                           HttpSession session){

        CommonResponse<AccountVO> response = accountService.login(username,password);
        if (response.isSuccess()){
            session.setAttribute("login_account",response.getData());
        }
        return response;
    }

    @PostMapping("/getAccount")
    @ResponseBody
    public CommonResponse<AccountVO> getAccount(HttpSession session){
        AccountVO accountVO = (AccountVO)session.getAttribute("login_account");
        if(accountVO != null){
            return CommonResponse.creatForSuccess(accountVO);
        }
        //return CommonResponse.creatForError(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,不能获取用户信息");
        return CommonResponse.creatForError("用户未登录,不能获取用户信息");
    }
}
